﻿/********************************************************************************************
 * 文件名称:	List.cs
 * 设计人员:	yanxianghui@gmail.com
 * 设计时间:	2009-07-26
 * 功能描述:	豆列
 *		
 *		
 * 注意事项:	
 *
 * 版权所有:	Copyright (c) 2007, Fujian SIRC
 *
 * 修改记录: 	修改时间		人员		修改备注
 *				----------		------		-------------------------------------------------
 *
 ********************************************************************************************/
using System;
using System.Collections.Generic;
using Shanfree.SharpMovie.DALFactory;
using Shanfree.SharpMovie.IDAL;
using Shanfree.SharpMovie.Model;
using SIRC.Framework.Net;
using System.IO;
using Shanfree.Framework.Utility;
using System.Text;

namespace Shanfree.SharpMovie.BLL
{
    /// <summary>
    /// 豆列
    /// </summary>
    public class List
    {
        private static readonly IList dal = DataAccess.CreateList();
        private static readonly IListItem idal = DataAccess.CreateItem();

        #region TranImport
        /// <summary>
        ///  imdb编号对应的电影导入数据库中
        /// </summary>
        /// <param name="listID">列表主键</param>
        /// <param name="txtfilePath">导入文件地址</param>
        /// <returns></returns>
        public void TranImport(string listID, string txtfilePath, string userID)
        {
            //映射文件地址
            string mapfilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"bin\EntityMapping.xml");
            EntityXpathInfo eInfo = SerializeHandler<EntityXpathInfo>.DeSerializeFromXMLFile(mapfilePath);
            string urlBase = eInfo.URL;
            using (StreamReader sr = new StreamReader(txtfilePath, Encoding.UTF8, true))
            {
                while (sr.EndOfStream == false)
                {
                    // todo：1927 忠次旅日记                       tt0342196
                    string ttCodeString = sr.ReadLine();
                    string remark = string.Empty;
                    string ttCode = string.Empty;
                    // 空白行
                    bool hasDate = getRemarkAndTTCode(ttCodeString, out remark, out ttCode);
                    if (hasDate == false)
                    {
                        // 查看下一行
                        continue;
                    }
                    string movieID = string.Empty;
                    movieID = new Movie().Import(eInfo, ttCode, userID);
                    // 添加到收藏分类
                    new ListItem().Add(listID, movieID, remark);
                }
            }
        }

        private bool getRemarkAndTTCode(string ttCodeString, out string remark, out string ttCode)
        {
            ttCodeString = ttCodeString.Replace("tt", "#sep#tt");
            string[] array = ttCodeString.Split(new string[] { "#sep#" }, StringSplitOptions.None);
            if (null == array || array.Length != 2)
            {
                remark = string.Empty;
                ttCode = string.Empty;
                return false;
            }
            remark = array[0].Trim();
            ttCode = array[1].Trim();
            return true;
        }

        #endregion

        /// <summary>
        /// 根据列表
        /// </summary>
        /// <param name="pageSize">页大小</param>
        /// <param name="pageIndex">当前页索引</param>
        /// <returns></returns>
        public IList<ListInfo> GetList(int pageSize, int pageIndex)
        {
            if (pageSize < 1)
            {
                throw new ArgumentException("页大小不能小于1.");
            }
            if (pageIndex < 0)
            {
                throw new ArgumentException("页索引不能小于0.");
            }
            return dal.GetList(pageSize, pageIndex);
        }

        /// <summary>
        /// 根据列表
        /// </summary>
        /// <param name="movieID">用户主键</param>
        /// <returns></returns>
        public IList<ListInfo> GetList(string userID, int pageSize, int pageIndex)
        {
            if (string.IsNullOrEmpty(userID))
            {
                throw new ArgumentNullException("userID");
            }
            if (pageSize < 1)
            {
                throw new ArgumentException("页大小不能小于1.");
            }
            if (pageIndex < 0)
            {
                throw new ArgumentException("页索引不能小于0.");
            }
            return dal.GetList(userID, pageSize, pageIndex);
        }
        /// <summary>
        /// 根据列表
        /// </summary>
        /// <param name="movieID">用户主键</param>
        /// <param name="isRecursion">是否递归获取子分类下所有列表</param>
        /// <returns></returns>
        public IList<ListInfo> GetListByCatalogID(string catalogID, bool isRecursion, int pageSize, int pageIndex)
        {
            if (string.IsNullOrEmpty(catalogID))
            {
                throw new ArgumentNullException("catalogID");
            }
            if (pageSize < 1)
            {
                throw new ArgumentException("页大小不能小于1.");
            }
            if (pageIndex < 0)
            {
                throw new ArgumentException("页索引不能小于0.");
            }
            return dal.GetListByCatalogID(catalogID, isRecursion, pageSize, pageIndex);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="menuID">ID</param>
        /// <returns></returns>
        public ListInfo GetByID(string id)
        {
            if (string.IsNullOrEmpty(id))
            {
                throw new ArgumentNullException("id");
            }
            return dal.GetByID(id);
        }
        /// <summary>
        /// 根据名称获取列表
        /// </summary>
        /// <param name="name">列表名称</param>
        /// <returns>列表实体，如不存在则实体ID为String.Empty</returns>
        public ListInfo GetByName(string name)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException("name");
            }
            return dal.GetByName(name);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="name">姓名</param>
        /// <param name="remark">备注</param>
        /// <param name="total">总数</param>
        /// <param name="menuID">栏目主键</param>
        /// <param name="userID">用户主键</param>
        /// <returns></returns>
        public string Add(string name, string remark, string total, string catalogID, string userID)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException("name不能为空。");
            }
            if (string.IsNullOrEmpty(remark))
            {
                throw new ArgumentNullException("remark不能为空。");
            }
            if (string.IsNullOrEmpty(userID))
            {
                throw new ArgumentNullException("userID不能为空。");
            }
            if (string.IsNullOrEmpty(catalogID))
            {
                throw new ArgumentNullException("catalogID不能为空。");
            }
            if (StringExtend.IsInt(total) == false)
            {
                throw new ArgumentNullException("total不能转化为数字。");
            }
            ListInfo nInfo = new ListInfo();
            nInfo.Name = name;
            nInfo.CreateTime = DateTime.Now;
            nInfo.Remark = remark;
            nInfo.Total = Convert.ToInt32(total);
            nInfo.CatalogID = catalogID;
            nInfo.UserID = userID;
            return dal.Add(nInfo);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id">主键</param>
        public void Delete(string id)
        {
            if (string.IsNullOrEmpty(id))
            {
                throw new ArgumentNullException("id");
            }
            ListInfo nInfo = new ListInfo();
            nInfo.ID = id;
            dal.Delete(nInfo);
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="id">记录主键</param>
        /// <param name="name">姓名</param>
        /// <param name="remark">备注</param>
        /// <param name="total">总数</param>
        /// <param name="catalogID">栏目主键</param>
        public void Update(string id, string name, string remark, string total, string catalogID)
        {
            if (string.IsNullOrEmpty(id))
            {
                throw new ArgumentNullException("主键不能为空。");
            }
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException("name不能为空。");
            }
            if (string.IsNullOrEmpty(remark))
            {
                throw new ArgumentNullException("remark不能为空。");
            }
            ListInfo nInfo = dal.GetByID(id);
            nInfo.ID = id;
            nInfo.Name = name;
            nInfo.Remark = remark;
            nInfo.Total = Convert.ToInt32(total);
            nInfo.CatalogID = catalogID;
            dal.Update(nInfo);
        }

        /// <summary>
        /// 根据列表ID获得影片项
        /// </summary>
        /// <param name="pageSize">页大小</param>
        /// <param name="pageIndex">当前页索引</param>
        /// <returns></returns>
        public IList<ListItemInfo> GetItemList(string listID, int pageSize, int pageIndex)
        {
            if (string.IsNullOrEmpty(listID))
            {
                throw new ArgumentNullException("listID不能为空。");
            }
            if (pageSize < 1)
            {
                throw new ArgumentException("页大小不能小于1.");
            }
            if (pageIndex < 0)
            {
                throw new ArgumentException("页索引不能小于0.");
            }
            return idal.GetList(listID, pageSize, pageIndex);
        }
    }
}
